﻿(function ($) {
    var popBox = [];
    var bodyBg = [];
    var iframeBg = [];
    var dragging = null;
    var offsetX = 0;
    var offsetY = 0;
    //鼠标拖动事件
    var mousemoveHandle = function (e) {
        //判断是否处于拖动状态
        if (dragging) {
            dragging.offset({
                top: e.pageY - offsetY,
                left: e.pageX - offsetX
            });
        }
    }
    //鼠标按下事件
    var mousedownHandle = function (e) {
        dragging = popBox;
        offsetX = e.pageX - dragging.offset().left;
        offsetY = e.pageY - dragging.offset().top;
    }
    //鼠标放开事件
    var mouseupHandle = function (e) {
        dragging = null;
    }
    $.extend({
        //是否IE6
        "isie6": function () {
            return /\bMSIE 6/.test(navigator.userAgent) && !window.opera;
        }
    });
    $.fn.extend({
        //显示弹出层
        "showBox": function (options) {
            popBox = this;
            var jQBody = $("body");
            var jQWindow = $(window);
            options = options || {};
            options.opacity = options.opacity || 0;//遮罩层透明度
            options.left = options.left || (jQWindow.width() / 2 - popBox.width() / 2);//弹出层的left，默认值为中间
            options.top = options.top || (jQWindow.height() / 2 - popBox.height() / 2);//弹出层的top，默认值为中间
            options.zindex = options.zindex || 1000; //遮罩的z-index值，弹出层+1，默认值1000
            //如果遮罩层不存在，则创建一个
            if (bodyBg.length < 1) {
                bodyBg = $('<div></div>');
            }
            //设置遮罩层样式
            bodyBg.css({
                "position": "absolute",
                "height": jQBody.height() + "px", //body的高度
                "width": jQBody.width() + "px", //body的宽度
                "z-index": options.zindex,
                "opacity": options.opacity,
                "background": "black"

            });
            jQBody.prepend(bodyBg);
            //IE6下需要iframe挡住下拉框ヽ(●-`Д´-)ノ 
            if ($.isie6()) {
                if (iframeBg.length < 1)
                    iframeBg = $('<iframe frameborder="0" src="about:blank" style="z-index:-1;width:' + jQBody.width() + 'px;height:' + jQBody.height() + 'px;filter:alpha(opacity=0);"></iframe>');
                bodyBg.prepend(iframeBg);
            }
            //调整要弹出div的样式，绝对定位，居中显示，位于遮罩上层
            popBox.css({
                "position": "absolute",
                "left": options.left + "px",
                "top":  options.top + "px",
                "z-index": options.zindex + 1

            });
            jQBody.append(popBox);
            //显示弹出div
            bodyBg.show();
            popBox.show();
            //绑定关闭事件
            $(".close", popBox).click(this.closeBox);
            //绑定拖动事件，为了防止重复绑定，先off
            var draggable = $(".draggable", popBox);
            draggable.off("mousedown", mousedownHandle);
            draggable.on("mousedown", mousedownHandle);
            jQBody.off("mousemove", mousemoveHandle);
            jQBody.on("mousemove", mousemoveHandle);
            jQBody.off("mouseup", mouseupHandle);
            jQBody.on("mouseup", mouseupHandle);
            return this;
        },
        //关闭弹出层，只需隐藏遮罩和弹出div
        "closeBox": function () {
            popBox.hide();
            bodyBg.hide();
            return this;
        }
    });
})(jQuery);